package com.jacobson.huobi.components

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.jacobson.core.widget.adapter.ErrorInfo
import com.jacobson.core.widget.adapter.ListRecyclerAdapter
import com.jacobson.huobi.R
import com.jacobson.huobi.common.expansion.load
import com.jacobson.huobi.common.expansion.toast
import kotlinx.android.synthetic.main.item_list_loading.view.*

/**
 * ================================================
 *
 *  @author Wucz on 2018/10/10 10:26
 * ================================================
 */
abstract class CommonListRecyclerAdapter<T>(context: Context) : ListRecyclerAdapter<T>(context) {
    override fun onCreateEmptyViewHolder(parent: ViewGroup): ViewHolder {
        return super.onCreateEmptyViewHolder(parent)
    }

    override fun onCreateLoadingViewHolder(parent: ViewGroup): ViewHolder {
        return LoadingViewHolder(context, parent)
//        return super.onCreateLoadingViewHolder(parent)
    }

    override fun onErrorWhenNotEmptyItems(errorInfo: ErrorInfo) {
        context?.toast("错误(${errorInfo.code}): ${errorInfo.message}")
    }

    class EmptyViewHolder(context: Context?, parent: ViewGroup) :
        ListRecyclerAdapter.ViewHolder(context, parent, R.layout.item_list_empty) {
        override fun onBindView(items: List<*>, position: Int) {
        }
    }

    class LoadingViewHolder(context: Context?, parent: ViewGroup) :
        ListRecyclerAdapter.ViewHolder(context, parent, R.layout.item_list_loading) {
        override fun onBindView(items: List<*>, position: Int) {
            itemView.run {
                itemListIvLoading.load { it.load(R.drawable.gif_list_loading) }
            }
        }
    }

    override fun isRequiredHolderFullSpan(holder: RecyclerView.ViewHolder): Boolean {
        return when (holder) {
            is LoadingViewHolder -> true
            else -> super.isRequiredHolderFullSpan(holder)
        }
    }
}